/**
 * 工作分配
 */
Ext.define('BenBen.modules.project.operation.OperationWorkDistributeWindow', {
	extend : 'Ext.window.Window',
	requires: [
       'BenBen.modules.system.organization.UserSearchWindow'
    ],
	width : 500,
    height : 500,
	initComponent : function() {
        var panel = this;
        
        Ext.define('dataModel', {
			extend : 'Ext.data.Model',
			fields : ['username','trueName'],
			idProperty : 'username'
		});
        var store = new Ext.create('Ext.data.Store', {
			model : 'dataModel',
			remoteSort : true,
			proxy: {
		        type: 'rest',
		        actionMethods: {
	                create : 'POST',
	                read   : 'GET',
	                update : 'POST',
	                destroy: 'POST'
	            },
                url : '',
		        reader: {
		            type: 'json',
		            root: 'data',
		            totalProperty: 'totalCount'
		        }
		     }
		});
        var gridPanel = Ext.create('Ext.grid.Panel', {
			selType: 'checkboxmodel',
            autoScroll : true,
			columns : [{
                xtype: 'rownumberer',
                width: 30,
                sortable: false,
                locked: true
            },{
                dataIndex: 'trueName',
                text: '工作执行人'
            }],
			height: 400,
			store : store,
			loadMask : true,
			tbar : [{
                text : '新增',
                iconCls : 'gridadd',
                handler : function(){
                	var userWin =  Ext.create('BenBen.modules.system.organization.UserSearchWindow',{
                		multiSelect : true,
                		departmentId : 586,
                    	distributeStore : store
                    });
                    
                    userWin.show();
                }
            },{
                text : '移除',
                iconCls : 'griddelete',
                handler : function(){
					var s = gridPanel.getSelectionModel().getSelection();
					for (var i = 0, r; r = s[i]; i++) {
						store.remove(r);
					}
                }
            }],
			viewConfig : {
				stripeRows : true,
				enableTextSelection : true
			}
		});
        
        var bbar = new Ext.Toolbar({
            items : [{
                xtype: 'tbfill'
            },{
                text : '确定',
                iconCls : 'formsave',
                handler : function() {
                	Ext.Ajax.request({
                        url: 'project/operationWork/distribute',
                        params: { 
                        	workId : panel.workId,
                        	handlerJson : convertJson(store.data.items)
                        },
                        method: 'POST',
                        callback: function (options, success, response) {
                            if (success) {
                                Ext.Msg.confirm('成功','分配成功');
                                panel.store.load();
                                panel.hide();
                            } else {
                                Ext.Msg.confirm('失败','分配失败');
                            }
                        }
                    });
                }
            }, {
                text : '取消',
                iconCls : 'formcancle',
                handler : function() {
                    panel.hide();
                }
            },{
                xtype: 'tbfill'
            }]
        });


		Ext.apply(this, {
			maximizable : true,
			closeAction : 'hide',
			layout : 'anchor',
            title : '工作分配',
            border : false,
            bbar : bbar,
			items : [gridPanel]
		});
		this.callParent(arguments);
	}
});